package com.abcd.godating.config;

import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.impl.transaction.SpringTransactionManager;
import org.neo4j.kernel.impl.transaction.UserTransactionImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.support.DelegatingGraphDatabase;
import org.springframework.data.neo4j.support.node.Neo4jHelper;
import org.springframework.data.neo4j.template.Neo4jTemplate;
import org.springframework.data.neo4j.transaction.ChainedTransactionManager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;

@Configuration
public class Neo4jConfig {
	public @Bean
	Neo4jTemplate neo4jTemplate(EmbeddedGraphDatabase graphDatabaseService,
			DataSourceTransactionManager transactionManager) {
		ChainedTransactionManager chainedTransactionManager = new ChainedTransactionManager(
				transactionManager, new JtaTransactionManager(
						new UserTransactionImpl(graphDatabaseService),
						new SpringTransactionManager(graphDatabaseService)));
		return new Neo4jTemplate(new DelegatingGraphDatabase(
				graphDatabaseService), chainedTransactionManager);
	}
	
	/*
	 * @author Ben
	 */
	@Bean(destroyMethod = "shutdown")
	public EmbeddedGraphDatabase graphDatabaseService() {
		EmbeddedGraphDatabase embeddedGraphDatabase = new EmbeddedGraphDatabase(
				"target/godating");
		// cleanDB for test env.
		Neo4jHelper.cleanDb(embeddedGraphDatabase);
		return embeddedGraphDatabase;
	}


}
